#!/usr/bin/perl
use warnings;
use Cwd;
use File::Copy;
use File::Path;
my $curdir=getcwd();
opendir(TEMP, "$curdir\/build") or die "can't open build";
my @filelist = readdir TEMP;
my $project_name;
my $config_file = "${curdir}\/make.ini";
my $project_ext;
my $key_name;
print "*****************************copy modem file begin******************************\n";
foreach(@filelist)
{
	if($_ =~ /^\./)
	{
		next;
	}
	else
	{
		$project_name = $_;
	}
}

if (-e $config_file) 
{
    open (FILE_HANDLE, "<$config_file") or die "cannot open $config_file\n";
    while (<FILE_HANDLE>) 
    {
      if (/^(\S+)\s*=\s*(\S+)/)
      {
      	$key_name = $1;
      	$project_ext = $2;
      	if ($key_name =~ /project/)
      	{
      		print "project_ext is $project_ext\n";
      	}
      }
		}
    close FILE_HANDLE;
}

my $path_1 = "${curdir}\/build\/${project_name}";
my $path_2 = "${curdir}\/tst\/database_classb_umts";
my $path_3 = "${curdir}\/make";
my $little_project_name = lc($project_name);
my $destination_path = "${curdir}\/${little_project_name}_$project_ext";
if (-e "$destination_path")
{
	rmtree($destination_path) || die "can not delete $destination_path";
}
mkdir $destination_path;
opendir(DIR1, $path_1) or die "Can't open build directory.\n";
foreach (readdir DIR1)
{
		if ($_ =~ /^DSP/)
		{
			copy("$path_1\/$_", $destination_path) or die "Copy failed: $!";
			print "********copy $_ successfull********\n";
		}
		if ($_ =~ /.bin/i)
		{
			copy("$path_1\/$_", "$destination_path/modem.img") or die "Copy failed: $!";
			print "********copy modem.img successfull********\n";
		}
		if ($_ =~ /^DbgInfo_/)
		{
			copy("$path_1\/$_", "$destination_path") or die "Copy failed: $!";
			print "********copy $_ successfull********\n";
		}
}

opendir(DIR2, $path_2) or die "Can't open build directory.\n";
foreach (readdir DIR2)
{
		if ($_ =~ /^BPLGUInfoCustomApp/)
		{
			copy("$path_2\/$_", $destination_path) or die "Copy failed: $!";
			print "********copy $_ successfull********\n";
		}
		if ($_ =~ /.bin/i)
		{
			copy("$path_2\/$_", "$destination_path") or die "Copy failed: $!";
			print "********copy $_ successfull********\n";
		}
}

opendir(DIR3, $path_3) or die "Can't open build directory.\n";
foreach (readdir DIR3)
{
		if ($_ =~ /MODEM.mak/)
		{
			copy("$path_3\/$_", "$destination_path/modem.mak") or die "Copy failed: $!";
			print "********copy modem.mak successfull********\n";
		}
}
print "*****************************copy modem file end********************************\n";